package com.whfc.common.third.zzkde;

import com.whfc.common.util.ByteUtil;
import com.whfc.common.util.JSONUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 履带吊数据解析
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-10-19 11:39
 */
public class CrawlerDataUtil {

    /**
     * 解析履带吊数据
     *
     * @param data
     * @return
     */
    public static CrawlerData parseCrawlerData(String data) {
        Assert.notNull(data, "原始数据不能为空");
        String[] strArr = data.split("\\*");

        CrawlerData crawler = new CrawlerData();

        crawler.setSn(strArr[4]);

        crawler.setMainHookWeight(Double.parseDouble(strArr[9]) / 100);
        crawler.setMainHookHeight(Double.parseDouble(strArr[10]) / 100);
        crawler.setViceHookWeight(Double.parseDouble(strArr[11]) / 100);
        crawler.setViceHookHeight(Double.parseDouble(strArr[12]) / 100);
        crawler.setTurnAngle(Double.parseDouble(strArr[13]) / 100);
        crawler.setRange(Double.parseDouble(strArr[14]) / 100);
        crawler.setTurnSpeed(Double.parseDouble(strArr[15]) / 100);
        crawler.setDipAngle(Double.parseDouble(strArr[16]) / 100);
        crawler.setWindSpeed(Double.parseDouble(strArr[17]) / 100);
        crawler.setLevelness(Double.parseDouble(strArr[18]) / 100);
        crawler.setMainHookMoment(Double.parseDouble(strArr[19]) / 100);
        crawler.setViceHookMoment(Double.parseDouble(strArr[20]) / 100);
        crawler.setConditions(Integer.parseInt(strArr[21]) / 100);
        crawler.setTotalOverloadCnt(Long.valueOf(strArr[25]));
        crawler.setTotalWindWorkCnt(Long.valueOf(strArr[26]));

        // （工作时长取值 / 100 = 工作小时） * 3600 = 工作时长秒
        Long workTimes = (Long.parseLong(strArr[27]) / 100) * 3600;

        crawler.setTotalWorkTimes(workTimes);
        crawler.setTotalWorkCnt(Long.valueOf(strArr[28]));
        crawler.setWorkStartTime(new Date(Long.parseLong(strArr[29]) * 1000));
        crawler.setWorkEndTime(new Date(Long.parseLong(strArr[30]) * 1000));
        crawler.setTotalMainCnt(Long.valueOf(strArr[31]));
        crawler.setTotalViceCnt(Long.valueOf(strArr[32]));
        crawler.setTotalTurnCnt(Long.valueOf(strArr[35]));
        crawler.setState(Integer.valueOf(strArr[38]));
        crawler.setAction(Integer.valueOf(strArr[39]));
        crawler.setTrip(Integer.valueOf(strArr[40]));
        crawler.setMinHeight(Double.parseDouble(strArr[41]) / 100);
        crawler.setMaxHeight(Double.parseDouble(strArr[42]) / 100);

        //解析报警
        Integer state = crawler.getState();
        crawler.setTurnWarn(ByteUtil.getBit(state, 0));
        crawler.setMainHookHeightWarn(ByteUtil.getBit(state, 2));
        crawler.setMainHookWeightWarn(ByteUtil.getBit(state, 3));
        crawler.setViceHookHeightWarn(ByteUtil.getBit(state, 4));
        crawler.setViceHookWeightWarn(ByteUtil.getBit(state, 5));
        crawler.setDipAngleWarn(ByteUtil.getBit(state, 6));
        crawler.setWindSpeedWarn(ByteUtil.getBit(state, 7));
        crawler.setAbnormalOpWarn(ByteUtil.getBit(state, 8));
        //防碰撞报警
        List<Integer> probeDistanceWarnList = new ArrayList<>();
        int probeDistanceWarn1 = ByteUtil.getBit(state, 9);
        probeDistanceWarnList.add(probeDistanceWarn1);
        int probeDistanceWarn2 = ByteUtil.getBit(state, 10);
        probeDistanceWarnList.add(probeDistanceWarn2);
        int probeDistanceWarn3 = ByteUtil.getBit(state, 11);
        probeDistanceWarnList.add(probeDistanceWarn3);
        int probeDistanceWarn4 = ByteUtil.getBit(state, 12);
        probeDistanceWarnList.add(probeDistanceWarn4);
        int probeDistanceWarn5 = ByteUtil.getBit(state, 13);
        probeDistanceWarnList.add(probeDistanceWarn5);
        int probeDistanceWarn6 = ByteUtil.getBit(state, 14);
        probeDistanceWarnList.add(probeDistanceWarn6);
        int probeDistanceWarn7 = ByteUtil.getBit(state, 15);
        probeDistanceWarnList.add(probeDistanceWarn7);
        int probeDistanceWarn8 = ByteUtil.getBit(state, 16);
        probeDistanceWarnList.add(probeDistanceWarn8);
        int probeDistanceWarn9 = ByteUtil.getBit(state, 17);
        probeDistanceWarnList.add(probeDistanceWarn9);
        int probeDistanceWarn10 = ByteUtil.getBit(state, 18);
        probeDistanceWarnList.add(probeDistanceWarn10);
        int probeDistanceWarn11 = ByteUtil.getBit(state, 19);
        probeDistanceWarnList.add(probeDistanceWarn11);
        int probeDistanceWarn12 = ByteUtil.getBit(state, 20);
        probeDistanceWarnList.add(probeDistanceWarn12);
        crawler.setProbeDistanceWarnList(probeDistanceWarnList);

        //解析动作
        Integer action = crawler.getAction();
        crawler.setTurnLeft(ByteUtil.getBit(action, 0));
        crawler.setTurnRight(ByteUtil.getBit(action, 1));
        crawler.setMainHookUp(ByteUtil.getBit(action, 4));
        crawler.setMainHookDown(ByteUtil.getBit(action, 5));
        crawler.setViceHookUp(ByteUtil.getBit(action, 6));
        crawler.setViceHookDown(ByteUtil.getBit(action, 7));
        crawler.setMainHookBrake(ByteUtil.getBit(action, 14));
        crawler.setViceHookBrake(ByteUtil.getBit(action, 15));

        //解析行程
        Integer trip = crawler.getTrip();
        crawler.setDipAngleLimit(ByteUtil.getBit(trip, 0));
        crawler.setDoorLimit(ByteUtil.getBit(trip, 1));
        crawler.setMainHookHeightUpperLimit(ByteUtil.getBit(trip, 2));
        crawler.setMainHookHeightLowerLimit(ByteUtil.getBit(trip, 3));
        crawler.setViceHookHeightUpperLimit(ByteUtil.getBit(trip, 4));
        crawler.setViceHookHeightLowerLimit(ByteUtil.getBit(trip, 5));

        //防碰撞
        // 防撞探头感应距离集合
        List<Double> probeDistanceList = new ArrayList<>();
        if (StringUtils.isNotBlank(strArr[5])) {
            probeDistanceList.add(Double.parseDouble(strArr[5]) / 100);
        }
        if (StringUtils.isNotBlank(strArr[6])) {
            probeDistanceList.add(Double.parseDouble(strArr[6]) / 100);
        }
        if (StringUtils.isNotBlank(strArr[7])) {
            probeDistanceList.add(Double.parseDouble(strArr[7]) / 100);
        }
        if (StringUtils.isNotBlank(strArr[8])) {
            probeDistanceList.add(Double.parseDouble(strArr[7]) / 100);
        }
        if (StringUtils.isNotBlank(strArr[22])) {
            probeDistanceList.add(Double.parseDouble(strArr[22]) / 100);
        }
        if (StringUtils.isNotBlank(strArr[23])) {
            probeDistanceList.add(Double.parseDouble(strArr[22]) / 100);
        }
        if (StringUtils.isNotBlank(strArr[24])) {
            probeDistanceList.add(Double.parseDouble(strArr[22]) / 100);
        }
        if (StringUtils.isNotBlank(strArr[33])) {
            probeDistanceList.add(Double.parseDouble(strArr[22]) / 100);
        }
        if (StringUtils.isNotBlank(strArr[34])) {
            probeDistanceList.add(Double.parseDouble(strArr[22]) / 100);
        }
        if (StringUtils.isNotBlank(strArr[36])) {
            probeDistanceList.add(Double.parseDouble(strArr[22]) / 100);
        }
        if (StringUtils.isNotBlank(strArr[37])) {
            probeDistanceList.add(Double.parseDouble(strArr[22]) / 100);
        }
        if (StringUtils.isNotBlank(strArr[43])) {
            probeDistanceList.add(Double.parseDouble(strArr[22]) / 100);
        }
        crawler.setProbeDistanceList(probeDistanceList);
        return crawler;
    }

    public static void main(String[] args) {
        //String data = "P*1*55*4*%d*1*2*16*6*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*%d*B*K\\r\\n";
        String data = "P*1*55*2*9903*1*2*18*5*41*3980*0*0*100*4218*0*4333*0*0*1750*0*0*0*0*0*0*1*5*3*1634291005*1634291009*0*0*0*0*0*0*0*1111*0*0*0*0*0*B*K";
        System.out.println("原始数据:" + data);
        CrawlerData crawlerData = CrawlerDataUtil.parseCrawlerData(data);
        System.out.println(JSONUtil.toPrettyString(crawlerData));
    }
}
